<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
class Cmoneyflow extends controller {
	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$dolist = C\router::get_url('admin-live-log-moneyflow', 'dolist');
		$dlang  = lang::inst('dd');
		$view->assign_by_ref('dolist', $dolist);
		$view->assign('rtype', $dlang->get('mf_rtype'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/log/moneyflow.ext.js');
	}
	
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$userid  = input::post('userid');
		if (!empty($userid)) {
			$where[] = array('userid', $userid, \classlib\orm::OP_EQ);
		}
		$anchorid  = input::post('anchorid');
		if (!empty($anchorid)) {
			$where[] = array('anchorid', $anchorid, \classlib\orm::OP_EQ);
		}
		$sdate  = input::post('sdate');
		if ($sdate) {
			$where[] = array('stime', strtotime($sdate), \classlib\orm::OP_GT);
		}
		$edate  = input::post('edate');
		if ($edate) {
			$where[] = array('stime', strtotime($edate), \classlib\orm::OP_LT);
		}
		$orm    = C\model::load('log.log.moneyflow');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$state= $orm->get_item($where, 'sum(coinprice) as coinprice, sum(earning) as earning');
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$uorm = C\model::load('pro.pl.userinfo');
			$gorm = C\model::load('pro.inc.gift');
			$morm = C\model::load('pro.inc.motor');
			$dlang= lang::inst('dd');
			$guard= $dlang->get('mf_guard');
			$vip  = $dlang->get('mf_vip');
			foreach ($list as &$item) {
				$user = $uorm->get_one($item['userid']);
				$item['usernick'] = $user['nickname'];
				$user = $uorm->get_one($item['anchorid']);
				$item['anchornick'] = $user['nickname'];
				if ($item['reftype'] == 1){
					$row = $gorm->get_one($item['refid']);
					$item['refname'] = $row['name'];
				} else if ($item['reftype'] == 2){
					$row = $morm->get_one($item['refid']);
					$item['refname'] = $row['name'];
				} else if ($item['reftype'] == 3){
					$item['refname'] = $vip[$item['refid']];
				} else if ($item['reftype'] == 4){
					$item['refname'] = $guard[$item['refid']];
				} else {
					$item['refname'] = '';
				}
				$item['stime'] = date('Y-m-d H:i:s', $item['stime']);
			}
			$list[] = array('anchorid'=>'statistics','coinprice'=>$state['coinprice'], 'earning'=>$state['earning']);
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}